package starfire.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.border.*;

import starfire.*;
import starfire.gui.siteTree.*;
import starfire.ftp.FtpSite;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author zzzhc.starfire@gmail.com
 * @version 1.0
 */

public class SiteManagerDialog
    extends JDialog {
  JPanel panel1 = new JPanel();
  JMenuBar menuBar = new JMenuBar();
  BorderLayout borderLayout1 = new BorderLayout();
  XPanel jPanel1 = new XPanel();
  JButton newButton = new JButton();
  JButton guideButton = new JButton();
  JPanel jPanel2 = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JSplitPane jSplitPane1 = new JSplitPane();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTree siteTree = new JTree();
  JPanel jPanel3 = new JPanel();
  JButton importButton = new JButton();
  JButton editButton = new JButton();
  JButton helpButton = new JButton();
  JButton connectButton = new JButton();
  JButton quitButton = new JButton();
  Border border1;
  Border border2;
  Border border3;
  JLabel hostLabel = new JLabel();
  JTextField labelField = new JTextField();
  JLabel siteAddressLabel = new JLabel();
  JTextField hostField = new JTextField();
  JLabel portLabel = new JLabel();
  JTextField portField = new JTextField();
  JLabel userLabel = new JLabel();
  JTextField userField = new JTextField();
  JLabel passwordLabel = new JLabel();
  JPasswordField passwordField = new JPasswordField();
  JPanel jPanel4 = new JPanel();
  Border border4;
  TitledBorder titledBorder1;
  ButtonGroup buttonGroup1 = new ButtonGroup();
  JRadioButton normalButton = new JRadioButton();
  JRadioButton anonymousButton = new JRadioButton();
  JRadioButton allButton = new JRadioButton();
  JMenu jMenu1 = new JMenu();
  JMenu jMenu2 = new JMenu();
  JMenu jMenu3 = new JMenu();
  JMenuItem jMenuItem1 = new JMenuItem();
  Border border5;
  JScrollPane jScrollPane2 = new JScrollPane();
  JTextArea descriptionArea = new JTextArea();
  Border border6;
  TitledBorder titledBorder2;
  Border border7;
  BorderLayout borderLayout3 = new BorderLayout();

  public SiteManagerDialog(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    try {
      jbInit();
    //  pack();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    border5 = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151)),BorderFactory.createEmptyBorder(2,2,2,2));
    border6 = BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151));
    titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151)),"Site Description");
    border7 = BorderFactory.createCompoundBorder(titledBorder2,BorderFactory.createEmptyBorder(1,1,1,1));
    portField.setToolTipText("");
    portField.setText("");
    anonymousButton.setSelected(true);
    anonymousButton.setBounds(new Rectangle(7, 44, 83, 25));
    jPanel1.setBorder(border5);
    siteTree.setFont(new java.awt.Font("Dialog", 0, 12));
    siteTree.setShowsRootHandles(false);
    jScrollPane2.setBorder(border7);
    jScrollPane2.setBounds(new Rectangle(124, 260, 159, 100));
    descriptionArea.setToolTipText("");
    descriptionArea.setText("");
    labelField.setText("");
    hostField.setText("");
    userField.setText("");
    quitButton.addActionListener(new SiteManagerDialog_quitButton_actionAdapter(this));
    jScrollPane1.getViewport().setBackground(Color.white);
    jPanel2.setLayout(borderLayout3);
    jPanel4.setLayout(null);
    allButton.setBounds(new Rectangle(8, 68, 37, 25));
    normalButton.setBounds(new Rectangle(8, 19, 59, 25));
    jSplitPane1.setEnabled(false);
    jPanel4.add(normalButton, null);
    jPanel4.add(anonymousButton, null);
    jPanel4.add(allButton, null);
    jPanel3.add(jScrollPane2, null);
    jScrollPane2.getViewport().add(descriptionArea, null);
    jPanel3.add(siteAddressLabel, null);
    jPanel3.add(portLabel, null);
    jPanel3.add(userLabel, null);
    jPanel3.add(passwordLabel, null);
    jPanel3.add(hostLabel, null);
    jPanel3.add(labelField, null);
    jPanel3.add(hostField, null);
    jPanel3.add(portField, null);
    jPanel3.add(userField, null);
    jPanel3.add(passwordField, null);
    border1 = BorderFactory.createEtchedBorder(Color.white,
                                               new Color(165, 163, 151));
    border2 = BorderFactory.createEtchedBorder(Color.white,
                                               new Color(165, 163, 151));
    border3 = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.white,
                                              Color.white,
                                              new Color(115, 114, 105),
                                              new Color(165, 163, 151));
    border4 = BorderFactory.createEmptyBorder();
    titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.
        white, new Color(165, 163, 151)), "Login type");
    setSize(530, 450);
    this.setResizable(false);
    this.setTitle("Site Manager");
    panel1.setLayout(borderLayout1);
    newButton.setActionCommand("New");
    newButton.setText("New");
    guideButton.setText("Guide");
    importButton.setText("Import");
    editButton.setText("Edit");
    helpButton.setText("Help");
    connectButton.setText("Connect");
    quitButton.setText("Quit");
    jScrollPane1.setBorder(border1);
    jPanel3.setBorder(border2);
    jPanel3.setPreferredSize(new Dimension(187, 296));
    jPanel3.setLayout(null);
    hostLabel.setHorizontalAlignment(SwingConstants.LEFT);
    hostLabel.setHorizontalTextPosition(SwingConstants.LEFT);
    hostLabel.setText("Label");
    hostLabel.setVerticalAlignment(SwingConstants.CENTER);
    hostLabel.setBounds(new Rectangle(12, 1, 26, 16));
    labelField.setSelectionStart(3);
    labelField.setHorizontalAlignment(SwingConstants.LEFT);
    labelField.setBounds(new Rectangle(9, 18, 228, 25));
    siteAddressLabel.setHorizontalAlignment(SwingConstants.LEFT);
    siteAddressLabel.setHorizontalTextPosition(SwingConstants.LEFT);
    siteAddressLabel.setText("Ftp site address");
    siteAddressLabel.setBounds(new Rectangle(8, 49, 78, 19));
    hostField.setDebugGraphicsOptions(0);
    hostField.setColumns(20);
    hostField.setHorizontalAlignment(SwingConstants.LEFT);
    hostField.setBounds(new Rectangle(9, 71, 230, 25));
    portLabel.setHorizontalAlignment(SwingConstants.LEFT);
    portLabel.setHorizontalTextPosition(SwingConstants.LEFT);
    portLabel.setText("Site Port");
    portLabel.setBounds(new Rectangle(10, 101, 40, 16));
    portField.setColumns(15);
    portField.setHorizontalAlignment(SwingConstants.LEFT);
    portField.setBounds(new Rectangle(9, 123, 125, 24));
    userLabel.setHorizontalAlignment(SwingConstants.LEFT);
    userLabel.setHorizontalTextPosition(SwingConstants.LEFT);
    userLabel.setText("User");
    userLabel.setBounds(new Rectangle(10, 156, 23, 16));
    userField.setColumns(20);
    userField.setBounds(new Rectangle(8, 176, 224, 26));
    passwordLabel.setText("password");
    passwordLabel.setBounds(new Rectangle(9, 207, 50, 16));
    passwordField.setColumns(20);
    passwordField.setBounds(new Rectangle(9, 230, 226, 29));
    jPanel4.setBorder(titledBorder1);
    jPanel4.setBounds(new Rectangle(9, 259, 100, 103));
    normalButton.setText("Normal");
    anonymousButton.setText("Anonymous");
    allButton.setText("All");
    jMenu1.setText("File");
    jMenu2.setText("Edit");
    jMenu3.setText("Security");
    jMenuItem1.setToolTipText("");
    jMenuItem1.setText("open");
    jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke('O',
        java.awt.event.KeyEvent.CTRL_MASK, false));
    jPanel3.add(jPanel4, null);
    getContentPane().add(panel1);
    panel1.add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(newButton);
    jPanel1.add(guideButton);
    panel1.add(jPanel2, BorderLayout.CENTER);
    jPanel2.add(jSplitPane1, BorderLayout.CENTER);
    jSplitPane1.add(jScrollPane1, JSplitPane.LEFT);
    siteTree.setModel(null);
   // siteTree.setModel(TreeXmlSupport.getSiteTreeRoot());
   //start one thread to load tree's model
    new LoadSite().start();
    jPanel1.add(importButton, null);
    jPanel1.add(editButton, null);
    jPanel1.add(helpButton, null);
    jPanel1.add(connectButton, null);
    jPanel1.add(quitButton, null);
    this.getRootPane().setJMenuBar(menuBar);
    jSplitPane1.add(jPanel3, JSplitPane.RIGHT);
    menuBar.add(jMenu1);
    menuBar.add(jMenu2);
    menuBar.add(jMenu3);
    jSplitPane1.setDividerLocation(200);
    buttonGroup1.add(normalButton);
    buttonGroup1.add(anonymousButton);
    buttonGroup1.add(allButton);
    jMenu1.add(jMenuItem1);
    addListener();
  }

  private void addListener()
  {
    siteTree.addMouseListener(new TreeMouseAdapter());
    siteTree.addTreeSelectionListener(new SiteTreeSelectionListener());
    connectButton.addActionListener(new ConnectListener());
  }

  public void doConnect()
  {
    Container c = getParent();
    if (c instanceof starfire.JFtpFrame)
    {
      final starfire.JFtpFrame frame = (starfire.JFtpFrame)c;
      TreePath path = siteTree.getSelectionPath();
      final TreeNode node = (TreeNode)path.getLastPathComponent();
      if (node instanceof SiteTreeNode)
      {
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {
            frame.createNewFtpMainPanel( ( (SiteTreeNode) node).getFtpSite());
          }
        });
      }
      setVisible(false);
      dispose();
    }

  }

  class SiteTreeSelectionListener implements TreeSelectionListener
  {
    public void valueChanged(TreeSelectionEvent event)
    {
      TreePath path = event.getPath();
      TreeNode node = (TreeNode)path.getLastPathComponent();
      if (node instanceof SiteTreeNode)
      {
        SiteTreeNode stn = (SiteTreeNode)node;
        FtpSite site = stn.getFtpSite();
        path = event.getNewLeadSelectionPath();
        node = (TreeNode)path.getLastPathComponent();
        site.setLabel(node.toString());
        stn.setFtpSite(site);
      }
    }
  }

  class ConnectListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      doConnect();
    }
  }



  class TreeMouseAdapter extends MouseAdapter
  {
    public void mouseClicked(MouseEvent event)
    {
      TreePath path = siteTree.getPathForLocation(
          event.getX(),event.getY());
      if (path==null)
        return;
      siteTree.setSelectionPath(path);
      TreeNode node = (TreeNode)path.getLastPathComponent();
      if (node==null)
        return;
      SiteTreeNode siteNode = null;
      FtpSite site = null;
      if (node instanceof SiteTreeNode)
      {
        siteNode = (SiteTreeNode)node;
        site = siteNode.getFtpSite();
        labelField.setText(site.getLabel());
        hostField.setText(site.getHost());
        portField.setText(String.valueOf(site.getPort()));
        userField.setText(site.getUser());
        passwordField.setText(site.getPassword());
        descriptionArea.setText(site.getDescript());
        if (site.getUser().equals(FtpSite.DEFAULTUSER))
        {
          anonymousButton.setSelected(true);
        }
        else
        {
          normalButton.setSelected(true);
        }
        if (event.getClickCount()==2)
        {
          doConnect();
        }
      }

    }
  }

  class LoadSite extends Thread
  {
    public void run()
    {
      siteTree.setModel(TreeXmlSupport.getSiteTreeRoot());
      jScrollPane1.getViewport().add(siteTree, BorderLayout.CENTER);
      jScrollPane1.updateUI();
    }
  }

  protected void finalize ()
  {
    if (siteTree==null)
      return;
    TreeModel model = siteTree.getModel();
    if (model!=null && model instanceof DefaultTreeModel)
    {
      TreeXmlSupport.writeSiteTree( (DefaultTreeModel) siteTree.getModel());
    }
  }

  void quitButton_actionPerformed(ActionEvent e) {
    this.setVisible(false);
    this.dispose();
  }

  class XPanel extends JPanel
  {
    public XPanel()
    {
      this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
    }
  }
}

class SiteManagerDialog_quitButton_actionAdapter implements java.awt.event.ActionListener {
  SiteManagerDialog adaptee;

  SiteManagerDialog_quitButton_actionAdapter(SiteManagerDialog adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.quitButton_actionPerformed(e);
  }
}
